package com.caifeng.mynotepad;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView clear, save, back;
    TextView noteTime, noteName;
    EditText ed;
    SQLiteHelper mSqLiteHelper;
    String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        initView();
        initData();
    }

    //控件
    public void initView() {
        clear = findViewById(R.id.clear);
        save = findViewById(R.id.save);
        back = findViewById(R.id.back);
        noteName = findViewById(R.id.name_text);
        noteTime = findViewById(R.id.note_time);
        ed = findViewById(R.id.note_content);

        clear.setOnClickListener(this);
        save.setOnClickListener(this);
        back.setOnClickListener(this);


    }
    //点击事件

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear://清除
                ed.setText("");
                break;
            case R.id.save://保存
                String noteContent = ed.getText().toString().trim();
                if (id != null) {
                    if (noteContent.length() > 0) {
                        if (mSqLiteHelper.updateData(id, noteContent, DBUtils.getTime())) {
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        } else {
                            showToast("失败了");
                        }
                    } else {
                        showToast("内容不能为空");
                    }
                } else {
                    if (noteContent.length() > 0) {
                        if (mSqLiteHelper.insertData(noteContent, DBUtils.getTime())) {
                            showToast("保存了");
                            setResult(2);
                            finish();
                        } else {
                            showToast("失败了");
                        }
                    }
                }
                break;
            case R.id.back://返回
                finish();
                break;
        }
    }

    //修改记事本
    protected void initData() {
        mSqLiteHelper = new SQLiteHelper(this);
        noteName.setText("添加");
        Intent intent = getIntent();
        if (intent != null) {
            id = intent.getStringExtra("id");
            if (id != null) {
                noteName.setText("修改");
                ed.setText(intent.getStringExtra("content"));
                noteTime.setText(intent.getStringExtra("time"));
                noteTime.setVisibility(View.VISIBLE);

            }
        }
    }

    //提示
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

}
